import axios from 'axios'
const qsLib = require('querystring')

import router from '../router'
// import { Message } from 'element-ui';
Toast.setDefaultOptions({ duration: 2000 });
axios.defaults.timeout = 10000;


axios.defaults.baseURL = 'http://localhost:3000';

//请求拦截  如果没有拿到token而且不是登录页,那么页面自动跳转到登录页
axios.interceptors.request.use(
  config => {
    config.timeout = 10000
    let token = localStorage.getItem('token');
    console.log("token", token);
    if (!token && window.location.pathname != '/login') {
      router.push('/login')
      return;
    }
  
    //如果有，则把token挂到请求头
    config.headers['User-Token'] = token;
    return config;
  },
  error => {
    return Promise.reject(error)
  }
)


//响应请求拦截
axios.interceptors.response.use(
  (response) => {
    // console.log(response);

    if (response && response.data && response.data.code == '401') {
        this.$message({
            type: 'warning',
            message: '没有权限!'
        });
        setTimeout(() => {
            console.log(response)
            router.push('/')
        }, 1000);

    }
    if (response && response.data && response.data.code == '-11') {
        this.$message({
            type: 'warning',
            message: '身份过期，请重新登陆'
        });
      setTimeout(() => {
        router.push({
          path: '/login'
        })
      }, 1000);

    }
    if (response && response.data && response.data.code == '-403') {
        this.$message({
            type: 'warning',
            message: '账户被冻结，请联系管理员'
        });
        setTimeout(() => {
            router.push({
            path: '/login'
            })
        }, 1000);
    }
    if (response && response.data && response.data.code == '-500') {
        this.$message({
            type: 'warning',
            message: '账户在别处登陆'
        });
        setTimeout(() => {
            router.push({
            path: '/login'
            })
        }, 1000);
    }

    if (response && response.data && response.data.code == '-399') {
        this.$message({
            type: 'warning',
            message: '没有权限'
        });
      setTimeout(() => {
        router.push({
          path: '/'
        })
      }, 1000);
      return;
    }

    if (response && response.data && response.data.code == '-200') {
        this.$message({
            type: 'warning',
            message: '账号异常'
        });
        setTimeout(() => {
            router.push({
            path: '/login'
            })
        }, 1000);
        return;
    }
    //拿到token
    if (response && response.headers && response.headers['user-token']) {
      localStorage.setItem('token', response.headers['user-token'])
    }
    if (response && response.data) {
      return response.data;
    } else {
      return response
    }


  },
  error => {
    if (error && error.response) {
      return Promise.reject(error);
    }
  }
)

export default {
  get ({url, params, headers }) {
    let options = {};
    if (params) options.params = params;
    if (headers) options.headers = headers;
    return axios.get(url, options);
  },

  post ({ url, data, headers }) {
    console.log("post")
    let options = {};
    // if (data) options.data = qsLib.stringify(data);
    if (headers) options.headers = headers;

    if (!(headers && headers['Content-Type'] && headers['Content-Type'] == 'multipart/form-data')) {
      data = qsLib.stringify(data);
    }

    return axios.post(url, data, options)
  }

}

